package com.lemon.smartsafety.utils.app;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.user.LoginOutEvent;
import com.lemon.smartsafetynew.entity.user.LoginNameVo;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.util.user.AccountUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Cooliean on 14-6-8.
 * SharedPreference 工具类
 */
public class SharedPreferencesHelper {
    private SharedPreferences        mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    /**
     * 实例化 SharedPreferenceHelper 用默认的名字 app_share
     *
     * @param context 当前上下文
     */
    public SharedPreferencesHelper(Context context) {
        mSharedPreferences = context.getSharedPreferences(Constants.SHAREPATH, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    /**
     * 实例化 SharedPreferenceHelper 用自定义名字
     *
     * @param context       当前上下文
     * @param shareFileName 自定义文件名
     */
    public SharedPreferencesHelper(Context context, String shareFileName) {
        mSharedPreferences = context.getSharedPreferences(shareFileName, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    public String getLoginName() {
        return mSharedPreferences.getString(Constants.LOGINNAME, "");
    }

    public SharedPreferencesHelper setLoginName(String loginName) {
        mEditor.putString(Constants.LOGINNAME, loginName);
        mEditor.commit();
        return this;
    }

    public String getLoginPassword() {
        return mSharedPreferences.getString(Constants.LOGIN_PASSWORD, "");
    }

    public SharedPreferencesHelper setLoginPassword(String value) {
        mEditor.putString(Constants.LOGIN_PASSWORD, value);
        mEditor.commit();
        return this;
    }

    public String getEncryptLoginPassword() {
        return mSharedPreferences.getString(Constants.ENCRYPT_LOGIN_PASSWORD, null);
    }

    public SharedPreferencesHelper setEncryptLoginPassword(String encryptLoginPassword) {
        mEditor.putString(Constants.ENCRYPT_LOGIN_PASSWORD, encryptLoginPassword);
        mEditor.commit();
        return this;
    }

    public String getUserId() {
        //change by Andrew.yin 2017/8/11 begin
        User user = AccountUtil.getUser();
        if(user != null) {
            if(user.getUserId() != null) {
                return user.getUserId();
            } else {
                return "";
            }
        } else {
            return "";
        }
        //return mSharedPreferences.getString(Constants.USER_ID, "");
        //change by Andrew.yin 2017/8/11 end
    }

    public SharedPreferencesHelper setUserId(String userId) {
        mEditor.putString(Constants.USER_ID, userId);
        mEditor.commit();
        return this;
    }

    public String getUserName() {
        return mSharedPreferences.getString(Constants.USER_NAME, "");
    }

    public SharedPreferencesHelper setUserName(String userName) {
        mEditor.putString(Constants.USER_NAME, userName);
        mEditor.commit();
        return this;
    }

    public String getUserPhotoUrl() {
        return mSharedPreferences.getString(Constants.USER_PHOTO_URL, null);
    }

    public SharedPreferencesHelper setUserPhotoUrl(String userPhotoUrl) {
        mEditor.putString(Constants.USER_PHOTO_URL, userPhotoUrl);
        mEditor.commit();
        return this;
    }


    public String getUserPhotoFid() {
        return mSharedPreferences.getString(Constants.USER_PHOTO_FID, null);
    }

    public SharedPreferencesHelper setUserPhotoFid(String userPhotoFid) {
        mEditor.putString(Constants.USER_PHOTO_FID, userPhotoFid);
        mEditor.commit();
        return this;
    }




    public String getDevicesId() {
        return mSharedPreferences.getString(Constants.DEVICES_ID, null);
    }

    public SharedPreferencesHelper setDevicesId(String devicesId) {
        mEditor.putString(Constants.DEVICES_ID, devicesId);
        mEditor.commit();
        return this;
    }

    public String getEncryptLoginInfo() {
        return mSharedPreferences.getString(Constants.ENCRYPT_LOGIN_INFO, null);
    }

    public SharedPreferencesHelper setEncryptLoginInfo(String encryptInfo) {
        mEditor.putString(Constants.ENCRYPT_LOGIN_INFO, encryptInfo);
        mEditor.commit();
        return this;
    }

    public String getEncryptLogininfoDeviceId() {
        return mSharedPreferences.getString(Constants.ENCRYPT_LOGININFO_DEVICEID, null);
    }

    public SharedPreferencesHelper setEncryptLogininfoDeviceId(String encryptInfo) {
        mEditor.putString(Constants.ENCRYPT_LOGININFO_DEVICEID, encryptInfo);
        mEditor.commit();
        return this;
    }

    public boolean isLogin() {
        return mSharedPreferences.getBoolean(Constants.IS_LOGIN_FLAG, false);
    }

    public SharedPreferencesHelper setIsLogin(boolean isLogin) {
        mEditor.putBoolean(Constants.IS_LOGIN_FLAG, isLogin);
        mEditor.commit();
        return this;
    }

    public String getUserType() {
        return mSharedPreferences.getString(Constants.USER_TYPE, "");
    }

    public SharedPreferencesHelper setUserType(String userType) {
        mEditor.putString(Constants.USER_TYPE, userType);
        mEditor.commit();
        return this;
    }

    public String getUserTypeName() {
        return mSharedPreferences.getString(Constants.USER_TYPE_NAME, "");
    }

    public SharedPreferencesHelper setUserTypeName(String string) {
        mEditor.putString(Constants.USER_TYPE_NAME, string);
        mEditor.commit();
        return this;
    }

    public String getUserPlaceInfo() {
        return mSharedPreferences.getString(Constants.USER_PLACE_INFO, null);
    }

    public SharedPreferencesHelper setUserPlaceInfo(String placeName) {
        mEditor.putString(Constants.USER_PLACE_INFO, placeName);
        mEditor.commit();
        return this;
    }


    public String getPlaceId() {
        return mSharedPreferences.getString(Constants.USER_PLACE_ID, null);
    }

    public SharedPreferencesHelper setPlaceId(String placeId) {
        mEditor.putString(Constants.USER_PLACE_ID, placeId);
        mEditor.commit();
        return this;
    }

    public String getPhoneToken() {
        return mSharedPreferences.getString(Constants.PHONE_TOKEN, "");
    }

    public SharedPreferencesHelper setPhoneToken(String token) {
        mEditor.putString(Constants.PHONE_TOKEN, token);
        mEditor.commit();
        return this;
    }

    public String getRayEyeUrl() {
        return mSharedPreferences.getString(Constants.KEY_FIRE_SUPERVISON_HOST, "");
    }

    public SharedPreferencesHelper setRayEyeUrl(String string) {
        mEditor.putString(Constants.KEY_FIRE_SUPERVISON_HOST, string);
        mEditor.commit();
        return this;
    }

    public String getEmail() {
        return mSharedPreferences.getString(Constants.EMAIL, "");
    }

    public SharedPreferencesHelper setEmail(String string) {
        mEditor.putString(Constants.EMAIL, string);
        mEditor.commit();
        return this;
    }

    public String getEmailValidate() {
        return mSharedPreferences.getString(Constants.EMAIL_VERIFY, "");
    }

    public SharedPreferencesHelper setEmailValidate(String string) {
        mEditor.putString(Constants.EMAIL_VERIFY, string);
        mEditor.commit();
        return this;
    }


    public String getBindLoginName() {
        return mSharedPreferences.getString(Constants.BIND_LOGIN_NAME, "");
    }

    public SharedPreferencesHelper setBindLoginName(String string) {
        mEditor.putString(Constants.BIND_LOGIN_NAME, string);
        mEditor.commit();
        return this;
    }




    public boolean isCloseFireListView() {
        return mSharedPreferences.getBoolean(Constants.IS_CLOSE_FIRE_LIST_VIEW, false);
    }

    public SharedPreferencesHelper setIsCloseFireListView(boolean bool) {
        mEditor.putBoolean(Constants.IS_CLOSE_FIRE_LIST_VIEW, bool);
        mEditor.commit();
        return this;
    }

    /**
     * 清除SharedPreferences
     */
    public SharedPreferencesHelper clear() {
        //保留登录名，和是否关闭返回视图
        String userName = getLoginName();
        boolean isClose = isCloseFireListView();


        mEditor.clear();
        mEditor.commit();
        setLoginName(userName);
        setIsCloseFireListView(isClose);

        return this;
    }

    /**
     * 退出登录
     */
    public SharedPreferencesHelper loginOut() {

        clear();
        AppService.getBus().post(new LoginOutEvent());
        return this;
    }
}
